package com.hrms.controller.employee;

import com.hrms.entity.*;
import com.hrms.http.ResponseResult;
import com.hrms.service.*;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

@Transactional
@RestController
@RequestMapping("/employee/roomstatus")
public class EmployeeRoomStatusController {
    // 员工客房状态

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private StayService stayService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private CustomerService customerService;

    @RequestMapping("init")
    public ResponseResult<List<Map<String, Object>>> init() {
        // 初始化
        List<Map<String, Object>> list = stayService.selectbyEmployeeInit();
        return new ResponseResult<>("SUCCESS", list);
    }

    @RequestMapping("/book") // 此方法已废弃
    public ResponseResult<Object> book(@RequestBody EntityUtil entity) {
        // 预订

        // 检查客房是否空闲
        Room roomBefore = new Room();
        roomBefore.setRoom_id(entity.getRoom_id());
        roomBefore.setRoom_status("空闲");

        if(!roomService.checkStatue(roomBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改客房状态
        Room roomAfter = new Room();
        roomAfter.setRoom_id(entity.getRoom_id());
        roomAfter.setRoom_status("待支付");

        if(!roomService.updateStatue(roomAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 新建订单
        Stay stay = new Stay();
        stay.setRoom_id(entity.getRoom_id());
        stay.setRoom_status("待支付");
        stay.setUser_id(entity.getUser_id());
        stay.setEmployee_id(entity.getEmployee_id());
        stay.setStay_start_time(entity.getStay_start_time());
        stay.setStay_end_time(entity.getStay_end_time());

        if(!stayService.add(stay)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/pay")
    public ResponseResult<Object> pay(@RequestBody EntityUtil entity) {
        // 支付订单
        Stay stay = new Stay();
        stay.setStay_id(entity.getStay_id());
        Integer stay_multiple_id_max = stayService.selectStayMultipleID(stay);

        for(int i = 1; i <= stay_multiple_id_max; i++) {
            stay.setStay_multiple_id(i);
            String room_id = stayService.selectRoom(stay);

            // 检查客房是否待支付
            Room roomBefore = new Room();
            roomBefore.setRoom_id(room_id);
            roomBefore.setRoom_status("待支付");

            if(!roomService.checkStatue(roomBefore)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 修改客房状态
            Room roomAfter = new Room();
            roomAfter.setRoom_id(room_id);
            roomAfter.setRoom_status("已入住");

            if(!roomService.updateStatue(roomAfter)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 绑定员工信息
            Stay stayAfter = new Stay();
            stayAfter.setStay_id(entity.getStay_id());
            stayAfter.setEmployee_id(entity.getEmployee_id());
            if(!stayService.updateEmployeeId(stayAfter)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/cancel")
    public ResponseResult<Object> cancel(@RequestBody EntityUtil entity) {
        // 退订
        Stay stay = new Stay();
        stay.setStay_id(entity.getStay_id());
        Employee employee = new Employee();
        employee.setEmployee_id(entity.getEmployee_id());

        // 入住人和房间号信息
        Integer stay_multiple_id_max = stayService.selectStayMultipleID(stay);
        String[] room_ids = new String[stay_multiple_id_max];
        String[] stay_names = new String[stay_multiple_id_max];

        for(int i = 1; i <= stay_multiple_id_max; i++) {
            stay.setStay_multiple_id(i);
            String room_id = stayService.selectRoom(stay);
            String stay_name = stayService.selectStayName(stay);
            room_ids[i - 1] = room_id;
            stay_names[i - 1] = stay_name;
        }

        // 用户相关信息
        Integer user_id = stayService.selectUser(stay);
        User user = new User();
        user.setUser_id(user_id);
        String customer_id = userService.selectCustomerId(user);
        Customer customer = new Customer();
        customer.setCustomer_id(customer_id);

        for(int i = 1; i <= stay_multiple_id_max; i++) {
            stay.setStay_multiple_id(i);

            // 检查客房是否待支付
            Room roomBefore = new Room();
            roomBefore.setRoom_id(room_ids[i - 1]);
            roomBefore.setRoom_status("待支付");

            if(!roomService.checkStatue(roomBefore)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 修改客房状态
            Room roomAfter = new Room();
            roomAfter.setRoom_id(room_ids[i - 1]);
            roomAfter.setRoom_status("空闲");

            if(!roomService.updateStatue(roomAfter)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 删除订单
            if(!stayService.delete(stay)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 新建历史订单
            Orders orders = new Orders();
            orders.setOrder_room_id(room_ids[i - 1]);
            orders.setOrder_room_type(roomService.selectRoomType(roomAfter));
            orders.setOrder_room_price(roomService.selectRoomPrice(roomAfter));
            orders.setOrder_customer_name(customerService.selectCustomerName(customer));
            orders.setOrder_user_id(user_id);
            orders.setOrder_customer_sex(customerService.selectCustomerSex(customer));
            orders.setOrder_customer_phone(customerService.selectCustomerPhone(customer));
            orders.setOrder_customer_id(customer_id);
            orders.setOrder_employee_name(employeeService.selectEmployeeName(employee));
            orders.setOrder_employee_id(employee.getEmployee_id());
            orders.setOrder_status("已退订");
            orders.setOrder_stay_name(stay_names[i - 1]);

            if(!ordersService.add(orders)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/continue")
    public ResponseResult<Object> Continue(@RequestBody EntityUtil entity) {
        // 续房
        Stay stay = new Stay();
        stay.setStay_id(entity.getStay_id());
        stay.setStay_end_time(entity.getStay_end_time());

        if(!stayService.updateEndTime(stay)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 绑定员工信息
        Stay stayAfter = new Stay();
        stayAfter.setStay_id(entity.getStay_id());
        stayAfter.setEmployee_id(entity.getEmployee_id());

        if(!stayService.updateEmployeeId(stayAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("checkout")
    public ResponseResult<Object> checkout(@RequestBody EntityUtil entity) {
        // 退房
        Stay stay = new Stay();
        stay.setStay_id(entity.getStay_id());
        Integer stay_multiple_id_max = stayService.selectStayMultipleID(stay);

        for(int i = 1; i <= stay_multiple_id_max; i++) {
            stay.setStay_multiple_id(i);
            String room_id = stayService.selectRoom(stay);

            // 检查客房是否已入住
            Room roomBefore = new Room();
            roomBefore.setRoom_id(room_id);
            roomBefore.setRoom_status("已入住");

            if(!roomService.checkStatue(roomBefore)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 修改客房状态
            Room roomAfter = new Room();
            roomAfter.setRoom_id(room_id);
            roomAfter.setRoom_status("退房中");

            if(!roomService.updateStatue(roomAfter)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 绑定员工信息
            Stay stayAfter = new Stay();
            stayAfter.setStay_id(entity.getStay_id());
            stayAfter.setEmployee_id(entity.getEmployee_id());

            if(!stayService.updateEmployeeId(stayAfter)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}